import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '@/views/LoginView.vue')
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import(/* webpackChunkName: "about" */ '@/views/RegisterView.vue')
  },
  {
    path: '/fukuan',
    name: 'fukuan',
    component: () => import(/* webpackChunkName: "about" */ '@/views/FukuanView.vue')
  },
  {
    path: '/setorder',
    name: 'setorder',
    component: () => import(/* webpackChunkName: "about" */ '@/views/SetOrderView.vue')
  },
  {
    path: '/scancode',
    name: 'scancode',
    component: () => import(/* webpackChunkName: "about" */ '@/views/ScanCodeView.vue')
  },
  {
    path: '/paysuccess',
    name: 'paysuccess',
    component: () => import(/* webpackChunkName: "about" */ '@/views/PaySuccessView.vue')
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/AboutView.vue')
  }
]



const router = new VueRouter({
  routes
})

//路由验证登录
router.beforeEach(function (to, from, next) {

  var user = localStorage.getItem("loginUser");

  if (to.path == '/reg') {
    next()
    return
  }

  if (user != null || to.path === '/login') {
    next();
  } else {
    if (user === null || user === '') {
      next('/login');
    }
  }
});

export default router
